package com.choosefine.it.basedata.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.TeamType;
import com.choosefine.it.basedata.model.TeamTypeClass;
import com.choosefine.it.basedata.model.vo.TeamTypeClassFindVO;
import com.choosefine.it.basedata.model.vo.TeamTypeVO;
import com.choosefine.it.basedata.service.TeamTypeClassService;
import com.choosefine.it.basedata.service.TeamTypeService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.DateUtils;
import com.github.pagehelper.PageInfo;

/**
 * 班组相关控制层
 * 
 * @author guwenwei 2016/11/20
 */
@RestController
@Api(value = "班组类别相关", description = "顾文伟")
@RequestMapping("/basedata/teamtype")
public class TeamTypeController extends BaseController {
	Logger logger = LoggerFactory.getLogger(TeamTypeController.class);
	
	@Autowired
	TeamTypeService teamtypeService;

	@Autowired
	TeamTypeClassService teamtypeClassService;

	/**
	 * 添加班组类别
	 * 
	 * @param teamtype
	 * @param response
	 * @return
	 * @throws BusinessException 
	 * @throws Exception
	 */
	@ApiOperation(value = "存储班组类别", notes = "传入班组类别对象，存储相关班组类别信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestBody TeamType teamtype) throws BusinessException {
		String result;
		if (teamtype != null) {
			teamtype.setCtime(DateUtils.getNowTime());
			Integer num = teamtypeService.insert(teamtype);
			if (num != null) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_CREATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_CREATE_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 更新班组类型数据
	 * 
	 * @param teamtype
	 *            类型名
	 * @return
	 * @throws BusinessException 
	 */
	@ApiOperation(value = "更改班组类型", notes = "传入班组类别对象，更改相关班组类别信息")
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody TeamType teamtype) throws BusinessException {
		String result = null;
		if (teamtype != null) {
			teamtype.setUtime(DateUtils.getNowTime());
			Integer num = teamtypeService.updateByPrimaryKeySelective(teamtype);
			if (num != null) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_UPDATE_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据ID获取班组类型名
	 * 
	 * @param id
	 *            班组类型ID
	 * @return
	 */
	@ApiOperation(value = "根据ID取班组类别", notes = "传入班组类别ID，根据ID获取班组类别信息")
	@ApiImplicitParam(name = "id", value = "班组类别ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getTeamTypeById(@PathVariable Integer id) {
		String result = null;
		if (id >= 0) {
			TeamType teamtype = teamtypeService.findByPrimaryKey(id);
			if (teamtype != null) {
				result = responseSuccess(teamtype);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据ID删除班组类型
	 * 
	 * @param id
	 *            班组类型ID
	 * @return
	 */
	@ApiOperation(value = "根据ID删除班组类别", notes = "传入班组类别ID，根据ID删除班组类别信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		String result = null;
		if (id != null) {
			Integer num = teamtypeService.deleteByPrimaryKey(id);
			if (num != null) {
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;

	}

	@ApiOperation(value = "获取大类中班组类型", notes = "根据父类ID获取班组类型列表")
	@ApiImplicitParam(name = "father", value = "班组大类ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/father/{father}", method = RequestMethod.GET)
	public String getTeamTypeByFather(@PathVariable Integer father) throws BusinessException {
		String result = null;
		if (father != null) {
			List<TeamType> teamTypeList = teamtypeService.getTeamTypeListByFather(father);
			result = responseArraySuccess(teamTypeList);
		}
		return result;
	}

	@ApiOperation(value = "获取所有班组类别", notes = "")
	@RequestMapping(value = "/teamtypelist", method = RequestMethod.GET)
	public String getTeamTypeList() throws BusinessException {
		String result = null;
		List<TeamType> teamtype = teamtypeService.selectAll();
		if (teamtype != null) {
			result = responseArraySuccess(teamtype);
		} else {
			result = responseFail(ResultCode.MSG_ERROR);
		}
		return result;
	}
	
	@ApiOperation(value = "获取所有大类和班组类别", notes = "")
	@RequestMapping(value = "/teamclassandtypelist", method = RequestMethod.GET)
	public String getWorkClassAndTypeList() throws BusinessException {
		String result = null;
		List<TeamTypeVO> teamtypevolist=null;
		List<TeamType> teamtypelist = null;
		List<TeamTypeClass> teamtypeclasslist = teamtypeClassService .selectAll();
		if(teamtypeclasslist!=null){
			teamtypevolist = new ArrayList<TeamTypeVO>(teamtypeclasslist.size());
			for (TeamTypeClass temp : teamtypeclasslist) {
				TeamTypeVO teamtypevo = new TeamTypeVO();
				teamtypevo.setSuperid(temp.getId());
				teamtypevo.setSupername(temp.getClassname());
				teamtypelist = teamtypeService.getTeamTypeListByFather(temp.getId());
				teamtypevo.setList(teamtypelist);
				teamtypevolist.add(teamtypevo);
			}
			result = responseArraySuccess(teamtypevolist);
		} else {
			result = responseFail(ResultCode.MSG_ERROR);
		}
		return result;
	}
	
	@ApiOperation(value = "根据名称获取班组类别", notes = "根据班组类别名和班组名获取工种")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "typeclassname", value = "用户类型", dataType = "String", required = false, paramType = "query"),
		@ApiImplicitParam(name = "typename", value = "用户编号", dataType = "String", required = false, paramType = "query"),
		@ApiImplicitParam(name = "pageNum", value = "起始页", dataType = "Integer", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页行", dataType = "Integer", required = true, paramType = "query")
	})
	@RequestMapping(value = "/teamtypewithname", method = RequestMethod.GET)
	public String getTeamTypeWithName(String typeclassname,String typename,Integer pageNum,Integer pageSize) {
		String result = null;
		TeamTypeClass teamtypeclass = null;
		Map<String, Object> map = new HashMap<>();
		if (typeclassname != null) {
			teamtypeclass = teamtypeClassService.getTeamTypeClassByName(typeclassname);
		}
		PageInfo<TeamType> teamtypelist;
		if (teamtypeclass != null) {
			if (teamtypeclass.getId() != null && !"".equals(teamtypeclass.getId())) {
				map.put("superId", teamtypeclass.getId());
			}
			if (typename != null && !"".equals(typename)) {
				map.put("name", typename);
			}
			teamtypelist = teamtypeService.getTeamTypeListByName(map, pageNum,
					pageSize);
		} else if (typename != null) {
			if (typename != null && !"".equals(typename)) {
				map.put("name", typename);
			}
			teamtypelist = teamtypeService.getTeamTypeListByName(map, pageNum,
					pageSize);
		} else {
			teamtypelist = teamtypeService.findAllByPage(null, pageNum, pageSize);
		}
		if (teamtypelist != null) {
			result = responseArraySuccess(teamtypelist);
		} else {
			result = responseFail(ResultCode.MSG_ERROR);
		}
		return result;
	}
	@ApiOperation(value = "班组类别排序")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "fromId", value = "移动项", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "toId", value = "移动到的位置", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "order", value = "移动标识（1上移-1下移）", dataType = "Integer", required = true, paramType = "path")
	})
	@RequestMapping(value = "/teamTypeOrder/{fromId}/{toId}/{order}", method = RequestMethod.PUT)
	public String teamTypeOrder(@PathVariable Integer fromId, @PathVariable Integer toId, @PathVariable Integer order){
		String result = null;
		int num = 0;
		if(order>0){
			num += teamtypeService.TeamTypeOrderUp(fromId, toId);
		}else{
			num += teamtypeService.TeamTypeOrderDown(fromId, toId);
		}
		if(num>0){
			result = responseSuccess(num);
		}else{
			result = responseFail();
		}
		return result;
	}
	
	/**
	 * @Description：根据班组类目id查询所有班组类别（陈健）
	 * @Author：chenjian
	 * @Create Date：2017年2月18日
	 * @param：
	 * @return：
	 */
	@ApiOperation(value = "根据班组类目id查询所有班组类别（陈健）")
	@RequestMapping(value = "/teamTypeSecList/{fatherId}", method = RequestMethod.GET)
	@ApiImplicitParam(name="fatherId",value="类目id",dataType="int",required=true,paramType="path")
	public String getTeamTypeSecList(@PathVariable Integer fatherId) {
		String result = null;
		List<TeamTypeClassFindVO> teamtypeList = teamtypeService.getTeamTypeSecList(fatherId);
		if (teamtypeList != null) {
			result = responseArraySuccess(teamtypeList);
		} else {
			result = responseFail(ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
	}
	
}
